package com.dnxh.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.dnxh.constant.CommonConstant;
import com.dnxh.constant.CustomResult;
import com.dnxh.entity.SendMeetingEmailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * The type Email util.
 *
 * @Author 枳湫
 * @Time 2022 /4/6 15:37
 * @Describe Basic_jar
 */
@Slf4j
@Component
public class EmailUtil {
	
	@Resource
	private TemplateEngine templateEngine;
	@Resource
	private JavaMailSender javaMailSender;
	private static final String FROM_EMAIL = "1933817759@qq.com";
	@Value ("${server.port}")
	private              int                port;
	/**
	 * 签到二维码
	 */
	private static final String             CHECK_IN_QR = "/common/email/qr?data=";
	
	/**
	 * 发送模板邮件
	 *
	 * @param email         需要发送到邮件
	 * @param subjectMatter 作用
	 * @param code          校验码
	 * @return the string
	 */
	public CustomResult sendThymeleafEmail (String email, String subjectMatter, String code){
		try {
			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
			helper.setSubject ("~验证邮件~");
			helper.setFrom (FROM_EMAIL);
			helper.setTo (email);
			Context context = new Context ();
			context.setVariable ("subject_matter",subjectMatter);
			context.setVariable ("code",code);
			String process = templateEngine.process ("authentication.html",context);
			helper.setText (process,true);
			javaMailSender.send (mimeMessage);
		} catch (MessagingException e) {
			e.printStackTrace ();
			return CustomResult.fail ("发送邮件失败!");
		}
		return CustomResult.success (CommonConstant.SUCCESS);
	}
	
	/**
	 * 发送会议通知
	 *
	 * @param email    the email
	 * @param name     the name
	 * @param time     the time
	 * @param location the location
	 * @param match    the match
	 */
	@Async("match-notice->")
	public void sendMatchNoticeEmail(String email,String name,String time,String location,String match,String other){
		try {
			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
			helper.setSubject ("~通知邮件~");
			helper.setFrom (FROM_EMAIL);
			helper.setTo (email);
			Context context = new Context ();
			context.setVariable ("name",name);
			context.setVariable ("time",time);
			context.setVariable ("location",location);
			context.setVariable ("match",match);
			context.setVariable ("other",other);
			String process = templateEngine.process ("matchNotice.html",context);
			helper.setText (process,true);
			javaMailSender.send (mimeMessage);
		} catch (MessagingException e) {
			e.printStackTrace ();
			log.error ("发送邮件失败!接收人:{}",email);
		}
	}
	
	@Async("send-email->")
	public void sendEmail(List<SendMeetingEmailVo> list){
		list.forEach (info -> {
			String qrUrl = IpUtil.getOutIpV4 () + StrUtil.COLON + port +
					CHECK_IN_QR + Base64.encode (info.getId () + StrUtil.EMPTY);
			String other = ",您的签到二维码为:" + qrUrl;
			this.sendMatchNoticeEmail (info.getEmail (),info.getName (),info.getTime (),info.getLocation (),info.getMeetingName (),other);
		});
	}
}
